package com.ywu.dev.admin.standard.service;

import com.ywu.boot.utils.RedisUtils;
import com.ywu.common.gson.GsonUtils;
import com.ywu.common.result.CommonResult;
import com.ywu.dev.admin.spi.entity.*;
import com.ywu.dev.admin.standard.cache.YwuServiceInfoCache;
import com.ywu.dev.admin.standard.data.YwuDevOpsCpuInfoRepository;
import com.ywu.dev.admin.standard.data.YwuDevOpsMemoryInfoRepository;
import com.ywu.dev.admin.standard.data.YwuDevOpsThreadInfoRepository;
import com.ywu.dev.admin.standard.dto.*;
import com.ywu.service.spi.dto.ServiceDto;
import com.ywu.service.spi.entity.ServiceEntity;
import com.ywu.service.spi.spi.ServiceSpi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName YwuDevOpsService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/11/24 12:38
 * @Version 1.0
 **/
@Service
@Slf4j
public class YwuDevOpsService {

    @Autowired
    private ServiceSpi serviceSpi;

    @Autowired
    private YwuServiceInfoCache ywuServiceInfoCache;

    @Autowired
    private YwuDevOpsCpuInfoRepository ywuDevOpsCpuInfoRepository;

    @Autowired
    private YwuDevOpsThreadInfoRepository ywuDevOpsThreadInfoRepository;

    @Autowired
    private YwuDevOpsMemoryInfoRepository ywuDevOpsMemoryInfoRepository;

    @Autowired
    private YwuDevOpsRecordService recordService;

    private static final String YWU_DEV_ADMIN_GC_INFO = "YWU_DEV_ADMIN_GC_INFO:";

    private static final String YWU_DEV_ADMIN_PROCESS_INFO = "YWU_DEV_ADMIN_PROCESS_INFO:";

    private static final String YWU_DEV_ADMIN_HEALTH_INFO = "YWU_DEV_ADMIN_HEALTH_INFO:";

    private static final String YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO = "YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO:";
    /**
    * @Author GroundDemo
    * @Description 查询微服务信息
    * @Date 12:39 2024/11/24
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 查询当前租户下的所有服务
        ServiceDto search = new ServiceDto();
        search.setApplicationName(ywuDevOpsDto.getServiceName());
        CommonResult<Object> allApplication = serviceSpi.getAllApplication(search);
        if (!"200".equals(allApplication.getCode())) {
            return CommonResult.error().setErrorCode("service query error");
        }
        Object data = allApplication.getData();
        String serialize = GsonUtils.serialize(data);
        List<ServiceEntity> serviceEntities = GsonUtils.deserializeList(serialize, ServiceEntity.class);
        if (CollectionUtils.isEmpty(serviceEntities)) {
            return CommonResult.success(new ArrayList<YwuServiceInfoDto>());
        }
        // 获取服务信息
        Map<String, List<ServiceInstance>> serviceInfoMap = ywuServiceInfoCache.queryServiceInfo();
        // 批量查询redis缓存 - 构建key
        List<String> redisKeys = new ArrayList<>();
        Set<String> instanceKey = serviceInfoMap.keySet();
        for (String key : instanceKey) {
            List<ServiceInstance> serviceInstances = serviceInfoMap.get(key);
            for (ServiceInstance instance : serviceInstances) {
                String tempKey = YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO + instance.getHost() + ":" + instance.getPort();
                redisKeys.add(tempKey);
            }
        }
        // 批量查询缓存
        List<String> redisList = RedisUtils.getByKeys(redisKeys);
        // 构建映射的map集合
        Map<String, String> redisInfoMap = new HashMap<>();
        for (int index = 0; index < redisKeys.size(); index++) {
            if (!StringUtils.isEmpty(redisList.get(index))) {
                // 非空才将当前数据添加到集合中，后续从集合中获取
                redisInfoMap.put(redisKeys.get(index), redisList.get(index));
            }
        }
        // 遍历租户服务，匹配服务信息，查询健康信息
        List<YwuServiceInfoDto> res = new ArrayList<>();
        for (ServiceEntity entity : serviceEntities) {
            if (serviceInfoMap.containsKey(entity.getApplicationName())) {
                List<ServiceInstance> serviceInstances = serviceInfoMap.get(entity.getApplicationName());
                for (ServiceInstance instance : serviceInstances) {
                    YwuServiceInfoDto ywuServiceInfoDto = new YwuServiceInfoDto();
                    ywuServiceInfoDto.setServiceId(String.valueOf(entity.getId()));
                    ywuServiceInfoDto.setServiceName(entity.getApplicationName());
                    String ipPort = instance.getHost() + ":" + instance.getPort();
                    ywuServiceInfoDto.setServiceIpPort(ipPort);
                    // 从缓存中获取服务的健康状态
                    if (redisInfoMap.containsKey(YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO + ipPort)) {
                        String redisInfo = redisInfoMap.get(YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO + ipPort);
                        YwuServiceDetailInfoEntity healthInfo = GsonUtils.deserialize(redisInfo, YwuServiceDetailInfoEntity.class);
                        ywuServiceInfoDto.setServiceStatus(healthInfo.getStatus());
                    } else {
                        ywuServiceInfoDto.setServiceStatus("DOWN");
                    }
                    res.add(ywuServiceInfoDto);
                }
            } else {
                YwuServiceInfoDto ywuServiceInfoDto = new YwuServiceInfoDto();
                ywuServiceInfoDto.setServiceId(String.valueOf(entity.getId()));
                ywuServiceInfoDto.setServiceName(entity.getApplicationName());
                // 不存在注册信息，则该服务已经宕机
                ywuServiceInfoDto.setServiceStatus("DOWN");
                res.add(ywuServiceInfoDto);
            }
        }
        return CommonResult.success(res);
    }

    /**
     * @Author GroundDemo
     * @Description 查询服务元数据
     * @Date 20:27 2024/11/24
     * @Param [ywuDevOpsDto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> queryServiceMetaDataInfo(YwuDevOpsDto ywuDevOpsDto) {
        Map<String, String> metaData = new HashMap<>();
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, metaData);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        // 转换为列表，后续表格展示
        List<MetaDataInfoDto> res = new ArrayList<>();
        Set<String> keys = metaData.keySet();
        for (String key : keys) {
            String value = metaData.get(key);
            MetaDataInfoDto metaDataInfoDto = new MetaDataInfoDto();
            metaDataInfoDto.setName(key);
            metaDataInfoDto.setValue(value);
            res.add(metaDataInfoDto);
        }
        return CommonResult.success(res);
    }

    /**
     * @Author GroundDemo
     * @Description 查询服务实例信息
     * @Date 20:22 2024/11/24
     * @Param [ywuDevOpsDto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> queryServiceDetailInfo(YwuDevOpsDto ywuDevOpsDto) {
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        // 从缓存中获取数据
        boolean hasKey = RedisUtils.hasKey(YWU_DEV_ADMIN_HEALTH_INFO + ywuDevOpsDto.getServiceIpPort());
        if (hasKey) {
            // 缓存存在，返回缓存中的数据
            String redisInfo = RedisUtils.getByKey(YWU_DEV_ADMIN_HEALTH_INFO + ywuDevOpsDto.getServiceIpPort());
            List<YwuServiceDetailInfoEntity> detail = GsonUtils.deserializeList(redisInfo, YwuServiceDetailInfoEntity.class);
            return CommonResult.success(detail);
        } else {
            return CommonResult.success(new ArrayList<>());
        }
    }

    /**
    * @Author GroundDemo
    * @Description 校验入参是否合法
    * @Date 20:34 2024/11/24
    * @Param [ywuDevOpsDto, metaData]
    * @return boolean
    **/
    private boolean checkServiceNameAndIp(YwuDevOpsDto ywuDevOpsDto, Map<String, String> metaData) {
        ServiceDto search = new ServiceDto();
        search.setApplicationName(ywuDevOpsDto.getServiceName());
        CommonResult<Object> allApplication = serviceSpi.getAllApplication(search);
        if (!"200".equals(allApplication.getCode())) {
            return false;
        }
        Object data = allApplication.getData();
        String serialize = GsonUtils.serialize(data);
        List<ServiceEntity> serviceEntities = GsonUtils.deserializeList(serialize, ServiceEntity.class);
        if (CollectionUtils.isEmpty(serviceEntities)) {
            return false;
        }
        List<ServiceEntity> collect = serviceEntities.stream().filter(res -> res.getApplicationName().equals(ywuDevOpsDto.getServiceName())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return false;
        }
        // 获取服务信息
        Map<String, List<ServiceInstance>> serviceInfoMap = ywuServiceInfoCache.queryServiceInfo();
        if (!serviceInfoMap.containsKey(ywuDevOpsDto.getServiceName())) {
            return false;
        }
        boolean flag = false;
        List<ServiceInstance> serviceInstances = serviceInfoMap.get(ywuDevOpsDto.getServiceName());
        for (ServiceInstance instance : serviceInstances) {
            if ((instance.getHost() + ":" + instance.getPort()).equals(ywuDevOpsDto.getServiceIpPort())) {
                flag = true;
                if (Objects.nonNull(metaData)) {
                    metaData.putAll(instance.getMetadata());
                }
                break;
            }
        }
        return flag;
    }

    /**
    * @Author GroundDemo
    * @Description 查询服务进程信息
    * @Date 13:01 2024/11/25
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceProcessInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 校验参数
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        // 从缓存中获取数据
        boolean hasKey = RedisUtils.hasKey(YWU_DEV_ADMIN_PROCESS_INFO + ywuDevOpsDto.getServiceIpPort());
        if (!hasKey) {
            // 缓存不存在，返回空值
            return CommonResult.success(new ArrayList<>());
        } else {
            String redisInfo = RedisUtils.getByKey(YWU_DEV_ADMIN_PROCESS_INFO + ywuDevOpsDto.getServiceIpPort());
            YwuDevOpsProcessInfoEntity entity = GsonUtils.deserialize(redisInfo, YwuDevOpsProcessInfoEntity.class);
            entity.setId("1");
            List<YwuDevOpsProcessInfoEntity> res = new ArrayList<>();
            res.add(entity);
            return CommonResult.success(res);
        }
    }

    /**
    * @Author GroundDemo
    * @Description 查询CPU使用率列表
    * @Date 19:38 2024/11/25
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceCpuInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 校验参数
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        // 获取要查询的时间段
        String searchDataStr = YwuDevOpsDto.getSearchDataStr(ywuDevOpsDto.getSearchTimeOver());
        YwuDevOpsCpuInfoEntity search = new YwuDevOpsCpuInfoEntity();
        search.setIpPort(ywuDevOpsDto.getServiceIpPort());
        search.setSamplingTime(searchDataStr);
        List<YwuDevOpsCpuInfoEntity> queryList = ywuDevOpsCpuInfoRepository.queryList(search);
        List<Map<String, Object>> res = new ArrayList<>();
        // 构建系统CPU使用率的信息
        HashMap<String, Object> systemCpuInfo = new HashMap<>();
        List<Map<String, Object>> systemCpuInfoList = new ArrayList<>();
        systemCpuInfo.put("name", "系统CPU使用率");
        systemCpuInfo.put("data", systemCpuInfoList);
        HashMap<String, Object> processCpuInfo = new HashMap<>();
        List<Map<String, Object>> processCpuInfoList = new ArrayList<>();
        processCpuInfo.put("data", processCpuInfoList);
        processCpuInfo.put("name", "进程CPU使用率");
        for (YwuDevOpsCpuInfoEntity entity : queryList) {
            HashMap<String, Object> tempMap = new HashMap<>();
            ArrayList<Object> tempList = new ArrayList<>();
            tempList.add(entity.getSamplingTime());
            tempList.add(entity.getSamplingValue());
            tempMap.put("value", tempList);
            if (entity.getType().equals("process")) {
                processCpuInfoList.add(tempMap);
            } else {
                systemCpuInfoList.add(tempMap);
            }
        }
        res.add(systemCpuInfo);
        res.add(processCpuInfo);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 查询服务GC信息
    * @Date 12:17 2024/11/26
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceGcInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 校验参数
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        //从缓存中获取数据
        boolean hasKey = RedisUtils.hasKey(YWU_DEV_ADMIN_GC_INFO + ywuDevOpsDto.getServiceIpPort());
        if (hasKey) {
            String redisInfo = RedisUtils.getByKey(YWU_DEV_ADMIN_GC_INFO + ywuDevOpsDto.getServiceIpPort());
            YwuDevOpsGcInfoEntity entity = GsonUtils.deserialize(redisInfo, YwuDevOpsGcInfoEntity.class);
            ArrayList<YwuDevOpsGcInfoEntity> res = new ArrayList<>();
            res.add(entity);
            return CommonResult.success(res);
        } else {
            // 缓存不存在，返回空值
            return CommonResult.success(new ArrayList<>());
        }
    }

    /**
    * @Author GroundDemo
    * @Description 查询服务线程相关信息
    * @Date 12:21 2024/11/26
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceThreadInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 校验参数
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        String searchDataStr = YwuDevOpsDto.getSearchDataStr(ywuDevOpsDto.getSearchTimeOver());
        YwuDevOpsThreadInfoEntity search = new YwuDevOpsThreadInfoEntity();
        search.setSamplingTime(searchDataStr);
        search.setIpPort(ywuDevOpsDto.getServiceIpPort());
        List<YwuDevOpsThreadInfoEntity> queryList = ywuDevOpsThreadInfoRepository.queryList(search);
        List<Map<String, Object>> res = new ArrayList<>();
        // 构建系统CPU使用率的信息
        HashMap<String, Object> threadLiveInfo = new HashMap<>();
        List<Map<String, Object>> threadLiveInfoList = new ArrayList<>();
        threadLiveInfo.put("data", threadLiveInfoList);
        threadLiveInfo.put("name", "活跃线程数");
        HashMap<String, Object> threadDaemonInfo = new HashMap<>();
        List<Map<String, Object>> threadDaemonInfoList = new ArrayList<>();
        threadDaemonInfo.put("data", threadDaemonInfoList);
        threadDaemonInfo.put("name", "守护线程数");
        HashMap<String, Object> threadPeakInfo = new HashMap<>();
        List<Map<String, Object>> threadPeakInfoList = new ArrayList<>();
        threadPeakInfo.put("data", threadPeakInfoList);
        threadPeakInfo.put("name", "峰值线程数");
        for (YwuDevOpsThreadInfoEntity entity : queryList) {
            HashMap<String, Object> tempMap = new HashMap<>();
            ArrayList<Object> tempList = new ArrayList<>();
            tempList.add(entity.getSamplingTime());
            tempList.add(entity.getSamplingValue());
            tempMap.put("value", tempList);
            switch (entity.getType()) {
                case YwuDevOpsThreadInfoEntity.THREAD_LIVE_TYPE:
                    threadLiveInfoList.add(tempMap);
                    break;
                case YwuDevOpsThreadInfoEntity.THREAD_DAEMON_TYPE:
                    threadDaemonInfoList.add(tempMap);
                    break;
                case YwuDevOpsThreadInfoEntity.THREAD_PEAK_TYPE:
                    threadPeakInfoList.add(tempMap);
                    break;
            }
        }
        res.add(threadLiveInfo);
        res.add(threadDaemonInfo);
        res.add(threadPeakInfo);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 查询服务的内存信息
    * @Date 13:39 2024/11/26
    * @Param [ywuDevOpsDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryServiceMemoryInfo(YwuDevOpsDto ywuDevOpsDto) {
        // 校验参数
        boolean checkRes = checkServiceNameAndIp(ywuDevOpsDto, null);
        if (!checkRes) {
            return CommonResult.error().setErrorCode("service param invalid");
        }
        String searchDataStr = YwuDevOpsDto.getSearchDataStr(ywuDevOpsDto.getSearchTimeOver());
        YwuDevOpsMemoryInfoEntity search = new YwuDevOpsMemoryInfoEntity();
        search.setSamplingTime(searchDataStr);
        search.setIpPort(ywuDevOpsDto.getServiceIpPort());
        List<YwuDevOpsMemoryInfoEntity> queryList = ywuDevOpsMemoryInfoRepository.queryList(search);
        Map<String, List<Map<String, Object>>> res = doMemory(queryList);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 处理内存信息
    * @Date 13:52 2024/11/26
    * @Param [queryList]
    * @return java.util.Map<java.lang.String,java.util.List<java.util.Map<java.lang.String,java.lang.Object>>>
    **/
    private Map<String, List<Map<String, Object>>> doMemory(List<YwuDevOpsMemoryInfoEntity> queryList) {
        List<Map<String, Object>> heapList = new ArrayList<>();
        List<Map<String, Object>> nonHeapList = new ArrayList<>();
        // 构建系统CPU使用率的信息
        HashMap<String, Object> heapMemoryUsedInfo = new HashMap<>();
        List<Map<String, Object>> heapMemoryUsedInfoList = new ArrayList<>();
        heapMemoryUsedInfo.put("data", heapMemoryUsedInfoList);
        heapMemoryUsedInfo.put("name", "已用内存");
        HashMap<String, Object> heapMemoryMaxInfo = new HashMap<>();
        List<Map<String, Object>> heapMemoryMaxInfoList = new ArrayList<>();
        heapMemoryMaxInfo.put("data", heapMemoryMaxInfoList);
        heapMemoryMaxInfo.put("name", "最大内存");
        HashMap<String, Object> heapMemoryCommitInfo = new HashMap<>();
        List<Map<String, Object>> heapMemoryCommitInfoList = new ArrayList<>();
        heapMemoryCommitInfo.put("data", heapMemoryCommitInfoList);
        heapMemoryCommitInfo.put("name", "预创建内存");
        HashMap<String, Object> nonHeapMemoryUsedInfo = new HashMap<>();
        List<Map<String, Object>> nonHeapMemoryUsedInfoList = new ArrayList<>();
        nonHeapMemoryUsedInfo.put("data", nonHeapMemoryUsedInfoList);
        nonHeapMemoryUsedInfo.put("name", "已用内存");
        HashMap<String, Object> nonHeapMemoryMaxInfo = new HashMap<>();
        List<Map<String, Object>> nonHeapMemoryMaxInfoList = new ArrayList<>();
        nonHeapMemoryMaxInfo.put("data", nonHeapMemoryMaxInfoList);
        nonHeapMemoryMaxInfo.put("name", "最大内存");
        HashMap<String, Object> nonHeapMemoryCommitInfo = new HashMap<>();
        List<Map<String, Object>> nonHeapMemoryCommitInfoList = new ArrayList<>();
        nonHeapMemoryCommitInfo.put("data", nonHeapMemoryCommitInfoList);
        nonHeapMemoryCommitInfo.put("name", "预创建内存");
        for (YwuDevOpsMemoryInfoEntity entity : queryList) {
            HashMap<String, Object> tempMap = new HashMap<>();
            ArrayList<Object> tempList = new ArrayList<>();
            tempList.add(entity.getSamplingTime());
            tempList.add(entity.getSamplingValue());
            tempMap.put("value", tempList);
            switch (entity.getType()) {
                case YwuDevOpsMemoryInfoEntity.MEMORY_HEAP_USED_TYPE:
                    heapMemoryUsedInfoList.add(tempMap);
                    break;
                case YwuDevOpsMemoryInfoEntity.MEMORY_HEAP_MAX_TYPE:
                    heapMemoryMaxInfoList.add(tempMap);
                    break;
                case YwuDevOpsMemoryInfoEntity.MEMORY_HEAP_COMMIT_TYPE:
                    heapMemoryCommitInfoList.add(tempMap);
                    break;
                case YwuDevOpsMemoryInfoEntity.MEMORY_NON_HEAP_USED_TYPE:
                    nonHeapMemoryUsedInfoList.add(tempMap);
                    break;
                case YwuDevOpsMemoryInfoEntity.MEMORY_NON_HEAP_MAX_TYPE:
                    nonHeapMemoryMaxInfoList.add(tempMap);
                    break;
                case YwuDevOpsMemoryInfoEntity.MEMORY_NON_HEAP_COMMIT_TYPE:
                    nonHeapMemoryCommitInfoList.add(tempMap);
                    break;
            }
        }
        heapList.add(heapMemoryUsedInfo);
        heapList.add(heapMemoryMaxInfo);
        heapList.add(heapMemoryCommitInfo);
        nonHeapList.add(nonHeapMemoryUsedInfo);
        nonHeapList.add(nonHeapMemoryMaxInfo);
        nonHeapList.add(nonHeapMemoryCommitInfo);
        Map<String, List<Map<String, Object>>> res = new HashMap<>();
        res.put("heapMemoryInfo", heapList);
        res.put("nonHeapMemoryInfo", nonHeapList);
        return res;
    }

    /**
    * @Author GroundDemo
    * @Description 服务动态上报信息
    * @Date 12:16 2025/3/25
    * @Param [entity]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Async
    public void serviceReportInfo(YwuDevOpsGlobalEntity entity) {
        // 将GC信息存入缓存
        YwuDevOpsGcInfoEntity gcInfo = entity.getGcInfo();
        RedisUtils.set(YWU_DEV_ADMIN_GC_INFO + gcInfo.getIpPort(), GsonUtils.serialize(gcInfo));
        // 将进程信息存入缓存
        YwuDevOpsProcessInfoEntity processInfo = entity.getProcessInfo();
        RedisUtils.set(YWU_DEV_ADMIN_PROCESS_INFO + processInfo.getIpPort(), GsonUtils.serialize(processInfo));
        // 将服务健康状态存入缓存中
        List<YwuServiceDetailInfoEntity> healthInfo = entity.getHealthInfo();
        RedisUtils.set(YWU_DEV_ADMIN_HEALTH_INFO + processInfo.getIpPort(), GsonUtils.serialize(healthInfo));
        // 构建一个简单的服务健康信息，后续方便批量查询服务健康状态
        YwuServiceDetailInfoEntity simpleHealth = new YwuServiceDetailInfoEntity();
        simpleHealth.setName(healthInfo.get(0).getName());
        simpleHealth.setStatus(healthInfo.get(0).getStatus());
        RedisUtils.set(YWU_DEV_ADMIN_SIMPLE_HEALTH_INFO + processInfo.getIpPort(), GsonUtils.serialize(simpleHealth));
        // 记录CPU采样信息
        recordService.recordCpuInfo(entity.getCpuInfo());
        // 记录线程采样信息
        recordService.recordThreadInfo(entity.getThreadInfo());
        // 记录GC采样信息
        recordService.recordMemoryInfo(entity.getMemoryInfo());
    }
}
